#ifndef SYNC_H
#define SYNC_H

#include"os_type.h"
#include"list.h"

class SpinLock
{
private:
    //共享变量
    uint32 bolt;
public:
    SpinLock();
    void initialize();
    void lock();    //进入临界区
    void unlock();  //离开临界区
};

class Semaphore
{
private:
    uint32 counter;
    List waiting;
    SpinLock semLock;
public:
    Semaphore();
    void initialize(uint32 counter);
    void P();
    void V();
};

class ReaderWriter
{
private:
    Semaphore resourceAccess;
    Semaphore readerCountAccess;
    uint32 readerCount;
public:
    ReaderWriter();
    void initialize(uint32 counter=1);
    void readerEnter();
    void readerExit();
    void writerEnter();
    void writerExit();
};

#endif